//
//  MGPresentationController.m
//  Express
//
//  Created by lhongl on 2025/9/25.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGPresentationController.h"
@interface MGPresentationController ()

@property (nonatomic, assign) CGRect frame;

@property (nonatomic, strong)  UIVisualEffectView *visualView;

@end
@implementation MGPresentationController

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController
                       presentingViewController:(nullable UIViewController *)presentingViewController
                                          frame:(CGRect)frame{
    if(self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController]){
        self.frame = frame;
    }
    return self;
}

#pragma mark
//在呈现过渡即将开始的时候被调用的，并且做一个 alpha 从0到1的渐变过渡动画。
- (void)presentationTransitionWillBegin {
    [self.containerView addSubview:self.visualView];
    if ([[self presentedViewController] transitionCoordinator]) {
        [[[self presentedViewController] transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>context) {
            [self.visualView setAlpha: 0.3];
        } completion:nil];
    }else{
        [self.visualView setAlpha: 0.3];
    }
    if(self.isActionBackgroundDismiss){
        UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disAction)];
        [self.visualView addGestureRecognizer:tapGest];
    }
}

//在呈现过渡结束时被调用的，使用它在过渡效果已结束但没有完成时移除半透明的黑色背景 View。
- (void)presentationTransitionDidEnd:(BOOL)completed {
    if (!completed) {
        [self.visualView removeFromSuperview];
    }
}

//以上就涵盖了我们的背景 View 的呈现部分。
- (void)dismissalTransitionWillBegin {
    if ( [[self presentedViewController] transitionCoordinator]) {
        [[[self presentedViewController] transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>context) {
            [self.visualView setAlpha:0.0];
        } completion:nil];
    }else{
        [self.visualView setAlpha:0.0];
    }
}

//需要在消失完成后移除背景 View。
- (void)dismissalTransitionDidEnd:(BOOL)completed {
    if (completed) {
        [self.visualView removeFromSuperview];
    }
}

/// 方法重载。被呈现的 view 并没有完全完全填充整个屏幕，而是很小的一个矩形。被呈现的 view 的过渡动画之后的最终位置，是由 UIPresentationViewController 来负责定义的。我们重载 frameOfPresentedViewInContainerView 方法来定义这个最终位置, 将决定PresentVC内容的位置
- (CGRect)frameOfPresentedViewInContainerView {
    self.presentedView.frame  = self.frame;
    return self.presentedView.frame;
}

#pragma mark 事件
- (void)disAction{
    [[self presentedViewController] dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark
- (UIVisualEffectView *)visualView{
    if(!_visualView){
        //使用UIVisualEffectView实现模糊效果
        UIBlurEffect *blur  = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
        _visualView.frame = self.containerView.bounds;
        _visualView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        _visualView.alpha = 0.0;
        _visualView.userInteractionEnabled = YES;
    }
    return _visualView;
}

@end
